/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.hibernate.model.dao;
import admin.hibernate.pojo.Loaisanpham;
import admin.hibernate.pojo.Sanpham;
import admin.hibernate.util.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;



/**
 *
 * @author Administrator
 */
public class LoaiSanPhamDAO 
{
    //<editor-fold defaultstate="collapsed" desc="1. HQL - Lấy danh sách loại sản phẩm">
    public static List<Loaisanpham> LayDanhSachLoaiSanPhamHQL()
    {
        List<Loaisanpham> arrLoaiSanPham = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            String hql = "from Loaisanpham";
            Query query = session.createQuery(hql);
            arrLoaiSanPham = query.list();
            session.close();
        }
        catch(HibernateException ex)
        {
            System.out.println(ex);
        }
        return arrLoaiSanPham;
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="2. Lấy danh sách sản phẩm theo loại sản phẩm">
    public static List<Sanpham> LayDanhSachSanPhamTheoLoaiHQL(String tenLoai)
    {
        List<Sanpham> arrSanPham = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            String hql = "select sp from Sanpham sp where sp.loaisanpham.tenTrang like :tenTrang";
            Query query = session.createQuery(hql);
            query.setString("tenTrang", tenLoai);
            arrSanPham = query.list();
            //session.close();
        }
        catch(HibernateException ex)
        {
            System.out.println(ex);
        }
        return arrSanPham;
    }
    //</editor-fold>
}
